package com.bdqn.zhao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.zhao.pojo.SysDistricts;
import com.bdqn.zhao.dao.SysDistrictsMapper;
import com.bdqn.zhao.pojo.SysDistricts;
import com.bdqn.zhao.service.SysDistrictsService;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 地区 服务实现类
 * </p>
 *
 * @author zhangsan
 * @since 2023-04-24
 */
@Service
public class SysDistrictsServiceImpl extends ServiceImpl<SysDistrictsMapper, SysDistricts> implements SysDistrictsService {
    @Autowired
    private SysDistrictsMapper sysDistrictsMapper;
    @Override
    public List<SysDistricts> sysDistrictsAll(){
        IPage<SysDistricts> sysDistrictsIPage=new Page<>(0,10);
        IPage<SysDistricts> sysDistrictsIPage1 = sysDistrictsMapper.selectPage(sysDistrictsIPage, null);
        return sysDistrictsIPage1.getRecords();
    }

    @Override
    public SysDistricts selectByi(int id) {
        return sysDistrictsMapper.selectById(id);
    }


    @Autowired
    private SysDistrictsMapper districtsMapper;

    // 园区列表  --查询出下拉列表各个省份地址
    @Override
    public String selectDistricts(HttpSession session) {
        Map<String, Object> map = new HashMap<>();
        QueryWrapper<SysDistricts> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deep", "0");
        List<SysDistricts> sysDistrictsList = list(queryWrapper);
        if (sysDistrictsList != null) {
            session.setAttribute("sysDistrictsList", sysDistrictsList);
            System.out.println(sysDistrictsList);
        }
        return "fore/register";
    }

    @Override
    public Object address(Integer id) {
        QueryWrapper<SysDistricts> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid", id);
        Map<String, Object> map = new HashMap<>();
        List<SysDistricts> sysDistricts = list(queryWrapper);
        if (sysDistricts != null) {
            map.put("success", true);
            map.put("sysDistrictsList", sysDistricts);
        }
        return map;
    }

    @Override
    public String selectOne(Integer id,HttpSession session) {
        SysDistricts sysDistricts= getById(id);
        session.setAttribute("districtsname",sysDistricts.getName());
        if(sysDistricts.getDeep()==2){
            SysDistricts sysDistricts1= getById(id);
            session.setAttribute("districtsname1",sysDistricts1.getName());
            if(sysDistricts1.getDeep()==2){
                SysDistricts sysDistricts2= getById(id);
                session.setAttribute("districtsname2",sysDistricts1.getName());
            }
        }
        return "for/bianji";
    }


    @Override
    public int del(int id) {
        return sysDistrictsMapper.deleteById(id);
    }

    @Override
    public List<SysDistricts> fuzzy(@PathVariable Integer pageNum,
                                    @PathVariable Integer pageSize,Integer deep, String name) {
        IPage<SysDistricts> page=new Page<>(pageNum,pageSize);
        QueryWrapper<SysDistricts> queryWrapper=new QueryWrapper<>();
        if (deep!=null&&name==null){
            queryWrapper.eq("deep",deep);
        } else if (deep!=null&&name!=null) {
            queryWrapper.eq("deep",deep);
            queryWrapper.eq("name",name);
        }
        IPage<SysDistricts> sysDistrictsIPage = sysDistrictsMapper.selectPage(page, queryWrapper);
        return sysDistrictsIPage.getRecords();
    }
}
